﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using Newtonsoft.Json.Linq;
using System.Data;
using TaoSH.Data;

namespace TaoSH.AppCode
{
    public class CommonHelp
    {
        /// <summary>
        /// 绑定类
        /// </summary>
        public class BindData
        {
            /// <summary>
            /// 绑定部门
            /// </summary>
            /// <param name="ddl"></param>
            public void BindBranch(DropDownList ddl, string strISALL = "N")
            {
                if (strISALL == "N")
                {
                    ddl.DataSource = new JH_Auth_BranchB().GetEntities(d => d.DeptCode != 0 && d.DeptCode != 1);

                }
                else
                {
                    ddl.DataSource = new JH_Auth_BranchB().GetEntities(d => d.DeptCode != 0);
                }
                ddl.DataTextField = "DeptName";
                ddl.DataValueField = "DeptCode";
                ddl.DataBind();
            }


            /// <summary>
            /// 绑定页码
            /// </summary>
            /// <param name="ddl"></param>
            /// <param name="Page"></param>
            public void BindPage(DropDownList ddl, int Page)
            {
                ddl.Items.Clear();
                if (Page == 0)
                {
                    ddl.Items.Add(new ListItem() { Text = "1", Value = "1" });
                }
                else
                {
                    for (int i = 1; i <= Page; i++)
                    {
                        ddl.Items.Add(new ListItem() { Text = i.ToString(), Value = i.ToString() });
                    }
                }

            }


            public void BindSPLB(DropDownList ddl)
            {
                ddl.DataSource = new PT_Pro_CategoryB().GetEntities(d => d.TopID == 0);
                ddl.DataTextField = "MenuTxt";
                ddl.DataValueField = "MenuTxt";
                ddl.DataBind();

            }
        }


        public class SessionHelper
        {
            /// <summary>
            /// 根据session名获取session对象
            /// </summary>
            /// <param name="name"></param>
            /// <returns></returns>
            public static object GetSession(string name)
            {
                return HttpContext.Current.Session[name];
            }
            /// <summary>
            /// 设置session,sharejs.com
            /// </summary>
            /// <param name="name">session 名</param>
            /// <param name="val">session 值</param>
            public static void SetSession(string name, object val)
            {
                HttpContext.Current.Session.Remove(name);
                HttpContext.Current.Session.Add(name, val);
            }
        }




        public class FormHelp
        {

            public JH_Auth_FormBase GetFormCodeByFormCode(string strFormCode)
            {
                return new JH_Auth_FormBaseB().GetEntity(d => d.FormCode == strFormCode);
            }



            /// <summary>
            /// 添加表单记录
            /// </summary>
            /// <param name="strFormTable"></param>
            /// <param name="strUserName"></param>
            /// <returns></returns>
            public int addForm(string strFormTable, string strUserName)
            {
                DataTable dtReturn = new JH_Auth_FormBaseB().GetDTByCommand(" INSERT INTO " + strFormTable + " ( CreateTime,CreateUser)VALUES('" + DateTime.Now + "','" + strUserName + "') select @@identity");
                if (dtReturn.Rows.Count > 0)
                {
                    return int.Parse(dtReturn.Rows[0][0].ToString());
                }
                else
                {
                    return 0;
                }
            }
            /// <summary>
            /// 删除表单数据
            /// </summary>
            /// <param name="intFormID"></param>
            /// <param name="strFormTable"></param>
            public void DelForm(int intFormID, string strFormTable)
            {
                try
                {
                    new JH_Auth_FormBaseB().ExsSql(" DELETE " + strFormTable + "   where  ID=" + intFormID);

                }
                catch (Exception)
                {

                }
            }

        }
    }
}